

#include "task_read_version.h"
#include "comm_utils.h"

ReadVersionTask::ReadVersionTask(EvSerial *serial, uint8_t type)
    : TaskCommBase(serial)
{
    type_ = type;
}

void ReadVersionTask::run()
{
    QString mssg = QString("读取版本...");
    emit messageLogged(LogUtils::makeLogBin(mssg));

    uint8_t major;
    uint8_t minor;
    uint8_t patch;
    uint32_t build_time;
    uint32_t firm_len;
    int ret = callGetVersion(type_, &major, &minor, &patch, &build_time, &firm_len);
    if (ret != 0) {
        mssg = QString("读取版本失败, 错误码: %1").arg(ret);
        emit messageLogged(LogUtils::makeLogBin(mssg));
        emit finished(false);
    } else {
        mssg = QString("-- 固件版本: v%1.%2.%3, 编译时间: %4, 固件长度: %5")
                .arg(major)
                .arg(minor)
                .arg(patch)
                .arg(build_time)
                .arg(firm_len);
        emit messageLogged(LogUtils::makeLogBin(mssg));
        emit messageLogged(LogUtils::makeLogBin("读取版本成功"));
        emit finished(true);
    }
}
